﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using DigitArchive;

namespace archiveOne
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            loadXMLFile();      

        }

        private void ShowDescription()
        {
            XElement root = XElement.Load("Archive.xml");
           
            Object selLicense = comboLicense.SelectedItem;
            Object selSection = comboSection.SelectedItem;
            Object selCategory = comboCategory.SelectedItem;
            Object selMonth = comboMonth.SelectedItem;
            Object selYear = comboYear.SelectedItem;
            Object selMedia = comboMedia.SelectedItem;

            //label1.Text = selLicense.ToString();
            //label2.Text = selSection.ToString();
            //label3.Text = selCategory.ToString();
            //label4.Text = selMonth.ToString();
            //label5.Text = selYear.ToString();
            //label6.Text = selMedia.ToString();

            IEnumerable<XElement> selDes =
                from el in root.Elements("software")
                where (string)el.Element("License") == selLicense.ToString() &&
                    (string)el.Element("Section") == selSection.ToString() &&
                    (string)el.Element("Category") == selCategory.ToString() &&
                    (string)el.Element("Month") == selMonth.ToString() &&
                    (string)el.Element("Year") == selYear.ToString() &&
                    (string)el.Element("Media") == selMedia.ToString()
                select el;

            string[] arrDes = new string[10000];
            int i = 0;

            foreach (XElement el in selDes)
            {
                arrDes[i] = (string)el.Element("Title");
                i++;
            }

            //MessageBox.Show("Your selection yields no results.");
            listResults.DataSource = arrDes;

           
        }

        private void loadXMLFile()
        {
            XElement root = XElement.Load("Archive.xml");
            string[] arrTypes = new string[6];
            arrTypes[0] = "License";
            arrTypes[1] = "Section";
            arrTypes[2] = "Category";
            arrTypes[3] = "Month";
            arrTypes[4] = "Year";
            arrTypes[5] = "Media";

            List<ComboBox> comboList = new List<ComboBox>();
            comboList.Add(comboLicense);
            comboList.Add(comboSection);
            comboList.Add(comboCategory);
            comboList.Add(comboMonth);
            comboList.Add(comboYear);
            comboList.Add(comboMedia);
                       
            IEnumerable<XElement> dbList =
                from el in root.Elements("software")
                orderby (string)el.Element("Name") descending
                select el;

            for(int k=0;k<6;k++)
            {
                BindValues(dbList,arrTypes[k],comboList[k]);
            }
          
        
        }

        private void BindValues(IEnumerable<XElement> dbList,string typeSection,ComboBox cb)
        {
            string[] arrSource = new string[400];
            int i = 0;

            foreach (XElement el in dbList)
            {
                if (arrSource.Contains((string)el.Element(typeSection)))
                    continue;
                else
                {
                    arrSource[i] = (string)el.Element(typeSection);
                    i++;
                }

               // Console.WriteLine((string)el.Element(typeSection));
            }

            cb.DataSource = arrSource;
            cb.SelectedIndex = 0;
        }



        private void btnSearch_Click(object sender, EventArgs e)
        {
            groupBox2.Visible = true;
            ShowDescription();
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Insert the Media in the primary removable drive and press OK");
            
            System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
            string driveLetter;
            foreach (System.IO.DriveInfo drive in drives)
            {
                if (drive.DriveType == System.IO.DriveType.CDRom)
                {
                    try
                    {
                        driveLetter = drive.Name;
                        System.Diagnostics.Process.Start(driveLetter + lblPathLoc.Text);
                    }
                    catch (Exception ex)
                    {
                        
                    }
                }
                    
            }
            
        }

        private void listResults_SelectedValueChanged(object sender, EventArgs e)
        {
            XElement root = XElement.Load("Archive.xml");
            
            Object selListItem = listResults.SelectedItem;
            lblSelectedSoftware.Text = selListItem.ToString();

            IEnumerable<XElement> titleDes =
                from el in root.Elements("software")
                where (string)el.Element("Title") == selListItem.ToString()
                select el;

            foreach (XElement el in titleDes)
            {
                txtDescription.Text = (string)el.Element("Description");
                lblMonthLoc.Text = (string)el.Element("Month") + (string)el.Element("Year");
                lblMediaLoc.Text = (string)el.Element("Media");
                lblPathLoc.Text = (string)el.Element("Path");
                lblLicenseLoc.Text = (string)el.Element("License");
                lblSizeLoc.Text = (string)el.Element("Size") + " MB";
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form abt = new AboutForm();
            abt.Show();

        }

        private void lnkFeedback_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("IExplore", "http://vamsisubhash.co.cc/digit-archive/");
        }

        private void provideFeedbackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("IExplore", "http://vamsisubhash.co.cc/digit-archive/");
        }
    }
}
